using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using BrightIdeasSoftware;

namespace FastObjectListViewDemo
{
    public partial class MainForm : Form
    {
        private FastObjectListView folv;
        private ButtonColumn buttonColumn;

        public MainForm()
        {
            InitializeComponent();
            InitializeListView();
            LoadData();
        }

        private void InitializeListView()
        {
            // 初始化FastObjectListView
            folv = new FastObjectListView();
            folv.Dock = DockStyle.Fill;
            folv.View = View.Details;
            folv.OwnerDraw = true;
            folv.FullRowSelect = true;

            // 添加普通列
            folv.AllColumns.Add(new OLVColumn("ID", "Id") { Width = 60 });
            folv.AllColumns.Add(new OLVColumn("名称", "Name") { Width = 150 });
            folv.AllColumns.Add(new OLVColumn("状态", "Status") { Width = 100 });

            // 添加自定义按钮列
            buttonColumn = new ButtonColumn("操作", "Actions") { Width = 210 };
            buttonColumn.Button1Text = "查看";
            buttonColumn.Button2Text = "编辑";
            buttonColumn.Button3Text = "删除";
            
            // 注册按钮点击事件
            buttonColumn.Button1Clicked += ButtonColumn_Button1Clicked;
            buttonColumn.Button2Clicked += ButtonColumn_Button2Clicked;
            buttonColumn.Button3Clicked += ButtonColumn_Button3Clicked;
            
            folv.AllColumns.Add(buttonColumn);

            // 注册绘制和鼠标事件
            //folv.CellToolTip += Folv_CellToolTip;
            folv.MouseDown += Folv_MouseDown;

            this.Controls.Add(folv);
        }

        private void LoadData()
        {
            // 加载示例数据
            var items = new List<SampleData>
            {
                new SampleData { Id = 1, Name = "产品A", Status = "活跃" },
                new SampleData { Id = 2, Name = "产品B", Status = "已下架" },
                new SampleData { Id = 3, Name = "产品C", Status = "活跃" },
                new SampleData { Id = 4, Name = "产品D", Status = "待审核" },
                new SampleData { Id = 5, Name = "产品E", Status = "活跃" }
            };

            folv.SetObjects(items);
        }

        // 绘制单元格中的按钮
        //private void Folv_CellToolTip(object sender, ToolTipEventArgs e)
        //{
        //    if (e.Column == buttonColumn)
        //    {
        //        e.DrawDefault = false;
        //        buttonColumn.DrawButtons(e.Graphics, e.Bounds, e.Item);
        //    }
        //}

        // 处理鼠标点击事件，判断点击了哪个按钮
        private void Folv_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                var hitTest = folv.HitTest(e.Location);
                //if (hitTest.Item != null && hitTest.SubItem != null && hitTest.SubItem.Column == buttonColumn)
                //{
                //    //buttonColumn.CheckButtonClick(hitTest.Item, e.Location, hitTest.SubItem.Bounds);
                //}
            }
        }

        // 按钮点击事件处理
        private void ButtonColumn_Button1Clicked(object sender, ButtonClickedEventArgs e)
        {
            var data = e.Item as SampleData;
            MessageBox.Show($"查看: {data?.Name} (ID: {data?.Id})");
        }

        private void ButtonColumn_Button2Clicked(object sender, ButtonClickedEventArgs e)
        {
            var data = e.Item as SampleData;
            MessageBox.Show($"编辑: {data?.Name} (ID: {data?.Id})");
        }

        private void ButtonColumn_Button3Clicked(object sender, ButtonClickedEventArgs e)
        {
            var data = e.Item as SampleData;
            if (MessageBox.Show($"确定要删除 {data?.Name} 吗?", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // 执行删除操作
                folv.RemoveObject(data);
            }
        }
    }

    // 自定义按钮列
    public class ButtonColumn : OLVColumn
    {
        // 按钮文本
        public string Button1Text { get; set; } = "按钮1";
        public string Button2Text { get; set; } = "按钮2";
        public string Button3Text { get; set; } = "按钮3";

        // 按钮点击事件
        public event EventHandler<ButtonClickedEventArgs> Button1Clicked;
        public event EventHandler<ButtonClickedEventArgs> Button2Clicked;
        public event EventHandler<ButtonClickedEventArgs> Button3Clicked;

        public ButtonColumn(string text, string aspectName) : base(text, aspectName)
        {

        }

        // 绘制三个按钮
        public void DrawButtons(Graphics g, Rectangle cellBounds, OLVListItem item)
        {
            // 计算每个按钮的位置和大小
            int buttonWidth = (cellBounds.Width - 10) / 3; // 减去间距
            int spacing = 3; // 按钮之间的间距

            // 按钮1
            Rectangle button1Rect = new Rectangle(
                cellBounds.Left + spacing,
                cellBounds.Top + 2,
                buttonWidth,
                cellBounds.Height - 4
            );
            DrawButton(g, button1Rect, Button1Text, item, 1);

            // 按钮2
            Rectangle button2Rect = new Rectangle(
                button1Rect.Right + spacing,
                cellBounds.Top + 2,
                buttonWidth,
                cellBounds.Height - 4
            );
            DrawButton(g, button2Rect, Button2Text, item, 2);

            // 按钮3
            Rectangle button3Rect = new Rectangle(
                button2Rect.Right + spacing,
                cellBounds.Top + 2,
                buttonWidth,
                cellBounds.Height - 4
            );
            DrawButton(g, button3Rect, Button3Text, item, 3);
        }

        // 绘制单个按钮
        private void DrawButton(Graphics g, Rectangle rect, string text, OLVListItem item, int buttonIndex)
        {
            // 按钮背景
            using (Brush brush = new SolidBrush(item.Selected ? SystemColors.Highlight : SystemColors.Control))
            {
                g.FillRectangle(brush, rect);
            }

            // 按钮边框
            using (Pen pen = new Pen(SystemColors.ControlDark))
            {
                g.DrawRectangle(pen, rect);
            }

            // 按钮文本
            Color textColor = item.Selected ? SystemColors.HighlightText : SystemColors.ControlText;
            TextFormatFlags flags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter | TextFormatFlags.SingleLine;
            TextRenderer.DrawText(g, text, item.Font, rect, textColor, flags);
        }

        // 检查是否点击了按钮并触发相应事件
        public void CheckButtonClick(OLVListItem item, Point mouseLocation, Rectangle cellBounds)
        {
            int buttonWidth = (cellBounds.Width - 10) / 3;
            int spacing = 3;

            // 计算三个按钮的区域
            Rectangle button1Rect = new Rectangle(
                cellBounds.Left + spacing,
                cellBounds.Top + 2,
                buttonWidth,
                cellBounds.Height - 4
            );

            Rectangle button2Rect = new Rectangle(
                button1Rect.Right + spacing,
                cellBounds.Top + 2,
                buttonWidth,
                cellBounds.Height - 4
            );

            Rectangle button3Rect = new Rectangle(
                button2Rect.Right + spacing,
                cellBounds.Top + 2,
                buttonWidth,
                cellBounds.Height - 4
            );

            // 判断点击了哪个按钮
            if (button1Rect.Contains(mouseLocation))
            {
                Button1Clicked?.Invoke(this, new ButtonClickedEventArgs(item.RowObject));
            }
            else if (button2Rect.Contains(mouseLocation))
            {
                Button2Clicked?.Invoke(this, new ButtonClickedEventArgs(item.RowObject));
            }
            else if (button3Rect.Contains(mouseLocation))
            {
                Button3Clicked?.Invoke(this, new ButtonClickedEventArgs(item.RowObject));
            }
        }
    }

    // 按钮点击事件参数
    public class ButtonClickedEventArgs : EventArgs
    {
        public object Item { get; }

        public ButtonClickedEventArgs(object item)
        {
            Item = item;
        }
    }

    // 示例数据类
    public class SampleData
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Status { get; set; }
    }

    partial class MainForm
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Name = "MainForm";
            this.Text = "FastObjectListView 按钮列示例";
            this.ResumeLayout(false);
        }
    }
}
